MENUS
A top level window can have a menubar Associated with it a menubar displays A list of top-level menu choices. each choice is Associated with a dropdown menu. This concept is implemented in java by the following classes.
MenuBar,Menu and MenuItem
A MenuBar contains one or more menu object constructors for menu.
Constructors
Menu( )
Menu(string)
Menu(string,Boolean)

MenuItem( )
MenuItem(string)
MenuItem(string,menushortcut)
Methods
void setEnabled(boolean)
boolean isEnabled( )
void setLabel(string)
string getLabel( )

CheckBox MenuItem

            CheckBoxMenuItem( )
CheckBoxMenuItem(string)             
CheckBoxMenuItem(string,boolean)

            boolean getState( )
void setState(boolean)
MenuItem add(menuItem)
Menu add(menu)

 

DialogBoxes:
They are similar to frame windows except that dialog Boxes are always childwindow of a top leval window.
Dialog(framewindow,Boolean)
Dialog(framewindow,string,Boolean)

WRITE A PROGRAM ON DIALOG BOX.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//Create a subclass of dialog.
class SampleDialog extends Dialog implements ActionListener
{
SampleDialog(Frame parent,String title)
{
super(parent,title,false);
setLayout(new FlowLayout());
setSize(300,200);
add(new Label("press this button."));
Button b;
add(b=new Button("Cancel"));
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
public void paint(Graphics g)
{
g.drawString("This is in the dialog box",10,70);
}
}
//Create a subclass of Frame
class MenuFrame extends Frame
{
String msg=" ";
CheckboxMenuItem debug,test;
MenuFrame(String title)
{
super(title);

                //create menu bar and add it to frame
MenuBar mbar=new MenuBar();
setMenuBar(mbar);

                //create the menu items
Menu file=new Menu("File");
MenuItem item1,item2,item3,item4,item5;
file.add(item1=new MenuItem("New..."));
file.add(item2=new MenuItem("Open..."));
file.add(item3=new MenuItem("Close..."));
file.add(item4=new MenuItem("-"));
file.add(item5=new MenuItem("Quit..."));
mbar.add(file);

                Menu edit=new Menu("Edit");
MenuItem item6,item7,item8,item9;
edit.add(item6=new MenuItem("Cut"));
edit.add(item7=new MenuItem("Copy"));
edit.add(item8=new MenuItem("Paste"));
edit.add(item9=new MenuItem("-"));

                Menu sub=new Menu("Special");
MenuItem item10,item11,item12;
sub.add(item10=new MenuItem("First"));
sub.add(item11=new MenuItem("Second"));
sub.add(item12=new MenuItem("Third"));
edit.add(sub);

                //these are checkable menu items
debug=new CheckboxMenuItem("Debug");
edit.add(debug);
test=new CheckboxMenuItem("Testing");
edit.add(test);
mbar.add(edit);

    //create an object to handle action and item events

   MyMenuHandler handler=new MyMenuHandler(this);

  //register it to receive those events
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);
item11.addActionListener(handler);
item12.addActionListener(handler);
debug.addItemListener(handler);
test.addItemListener(handler);

                //create an object to handle window events
MyWindowAdapter adapter=new MyWindowAdapter(this);

                //register it to recive those events
addWindowListener(adapter);
}
public void paint(Graphics g)
{
g.drawString(msg,10,200);
if(debug.getState())
g.drawString("Debug is on.",10,220);
else
g.drawString("Debug is off.",10,220);
if(test.getState())
g.drawString("Testing is on.",10,240);
else
g.drawString("Testing is oFF.",10,240);
}
}
class MyWindowAdapter extends WindowAdapter
{
MenuFrame menuFrame;
public MyWindowAdapter(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
public void windowClosing(WindowEvent we)
{
menuFrame.setVisible(false);
}
}
class MyMenuHandler implements ActionListener,ItemListener
{
MenuFrame menuFrame;
public MyMenuHandler(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
//Handle action events
public void actionPerformed(ActionEvent ae)
{
String msg="You Selected";
String arg=(String)ae.getActionCommand();

                //activate a dialog box when new is selected
if(arg.equals("New..."))
{
msg+="New.";
SampleDialog d=new
SampleDialog(menuFrame,"new dialog box");
d.setVisible(true);
}
//try defining other dialog boxex for three options
else
if(arg.equals("Open..."))
msg+="Open.";
else
if(arg.equals("Close..."))
msg+="Close.";
else
if(arg.equals("Quit..."))
msg+="Quit.";
else
if(arg.equals("Edit"))
msg+="Edit.";
else
if(arg.equals("Cut"))
msg+="Cut.";
else
if(arg.equals("COpy"))
msg+="Copy.";
else
if(arg.equals("Paste"))
msg+="Paste.";
else
if(arg.equals("First"))
msg+="First.";
else
if(arg.equals("Second"))
msg+="Second.";
else
if(arg.equals("Third"))
msg+="Third.";
else
if(arg.equals("Debug"))
msg+="Debug.";
else
if(arg.equals("Testing"))
msg+="Testing.";
menuFrame.msg=msg;
menuFrame.repaint();
}
public void itemStateChanged(ItemEvent ie)
{
menuFrame.repaint();
}
}
//create frame window
public class Gbox extends Applet
{
Frame f;
public void init()
{
f=new MenuFrame("Menu Demo");
int width=Integer.parseInt(getParameter("Width"));
int height=Integer.parseInt(getParameter("height"));
setSize(new Dimension(width,height));
f.setSize(width,height);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
public void stop()
{
f.setVisible(false);
}
}

/*<applet code=Gbox width=250 height=250>
</applet>*/